<?php

namespace App\Http\Controllers\admin\doctor;

use App\Http\Controllers\Controller;
use App\Jobs\LoginDelayJob;
use App\Jobs\QueueAuth;
use App\Logic\doctor\DoctorLogic;
use App\Models\Doctor;
use App\Models\DoctorType;
use App\Server\EsService;
use Illuminate\Http\Request;
use App\Server\ApiService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class DoctorController extends Controller
{
    /**
     * Name: 展示页面
     * User: 王梓墨
     * Date: 2023/6/18
     * Time:20:34
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        return DoctorLogic::doctorList();
    }

    /**
     * Name: 浏览器展示页面
     * User: 王梓墨
     * Date: 2023/6/18
     * Time:20:34
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function pcIndex(Request $request)
    {
        /*$params = $request->all();
        if (!empty($params))
        {
            //医师姓名
            $name = $params['name'];
            //医师类别
            $type_name = $params['type_name'];
            $where = [];
            if (isset($name) || !empty($name)) {
                $where[] = ['name', 'like', "%$name%"];
            }
            if (isset($type_name) || !empty($type_name)) {
                $where[] = ['type_name', 'like', "%$type_name%"];
            }
            $data = Doctor::where($where)->paginate(5);
            $type = DoctorType::all();
            return view('doctor.pc_doctor_index', compact('data', 'type','name','type_name'));
        } else {
            $data = Doctor::paginate(5);
            $type = DoctorType::all();
            return view('doctor.pc_doctor_index', compact('data', 'type'));
        }*/

//        print_r($res);die();
        $name=$request->get('name');
        //使用es 搜索
        if ($name){
            $data=EsService::getInstance()->esSearch($name);
//            dd($data);
            $type = DoctorType::all();
            //执行rabbitmq php artisan queue:work rabbitmq --queue=test
            LoginDelayJob::dispatch()->delay(LoginDelayJob::startAfter())->onConnection('rabbitmq')->onQueue('test');
//            QueueAuth::dispatch($type)->onConnection('rabbitmq')->onQueue('test');
            return view('doctor.pc_doctor_index',compact('data','type','name'));
        }else{
            $data=Doctor::where('name','like',"%$name%")->paginate(10);
            $type = DoctorType::all();
            LoginDelayJob::dispatch()->delay(LoginDelayJob::startAfter());
            return view('doctor.pc_doctor_index',compact('data','type'));
        }
    }

    /**
     * Name: 获取医生详情信息
     * User: 王梓墨
     * Date: 2023/6/18
     * Time:20:35
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function doctorMessage($id)
    {
        $data = Doctor::where('id',$id)->first()->toArray();
        if ($data)
        {
            return ApiService::success('获取医师详情成功',$data);
        } else {
            return ApiService::fail('获取医师详情失败');
        }
    }

    /**
     * Name: 小程序分页展示
     * User: 王梓墨
     * Date: 2023/6/19
     * Time:10:03
     * @return \Illuminate\Http\JsonResponse
     */
    public function paging()
    {
        $data = Doctor::paginate(10);
        if ($data)
        {
            return ApiService::success('分页列表获取成功',$data);
        } else {
            return ApiService::fail('分页列表获取失败');
        }
    }

    /**
     * Name: 将数据表中的数据添加到es中
     * User: 王梓墨
     * Date: 2023/6/21
     * Time:10:26
     */
    public function mysqlCreate()
    {
        $data = Doctor::all()->toArray();
        try {
            foreach ($data as $v)
            {
                $res = EsService::getInstance()->esCreate($v);
            }
            return ApiService::success('添加成功',$res);
        } catch (\Exception $e) {
            Log::info($e->getMessage());
        }
    }
}
